using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rss;
namespace RSSClient
{
    public partial class Client : Form
    {
        private Cursor m_cursorOld = Cursors.Default;
        private MergedSearch.SearchWebAndLiveExpo_WebService search = new RSSClient.MergedSearch.SearchWebAndLiveExpo_WebService();
        public Client()
        {
            InitializeComponent();
            search.MergedSearchCompleted += new RSSClient.MergedSearch.MergedSearchCompletedEventHandler(search_MergedSearchCompleted);
        }

        void search_MergedSearchCompleted(object sender, RSSClient.MergedSearch.MergedSearchCompletedEventArgs e)
        {
            PopulateGridWithResults((string)e.Result);
            this.Cursor = m_cursorOld;
        }



        private void PopulateGridWithResults(string result)
        {





            Rss.RssFeed feed = new Rss.RssFeed(result);

            if (feed.Channels.Count != 0)
            {



                dgvResults.Rows.Clear();

                int gridRowNbr = 0;

                foreach (RssItem item in feed.Channels[0].Items)
                {

                    ///---------------------------------------
                    ///
                    DataGridViewRow row = new DataGridViewRow();
                    DataGridViewTextBoxCell source = new DataGridViewTextBoxCell();
                    DataGridViewTextBoxCell title = new DataGridViewTextBoxCell();




                    if (gridRowNbr % 2 == 0)
                    {
                        row.DefaultCellStyle.BackColor = Color.LightSteelBlue;
                    }
                    else
                    {
                        row.DefaultCellStyle.BackColor = Color.AliceBlue;
                    }

                    switch (item.Source.ToUpper())
                    {


                        case "LIVEEXPO":
                            source.Value = "Live Expo";
                            break;

                        case "LIVESEARCH":
                        case "":
                            source.Value = "Search";
                            break;

                    }



                    source.Tag = item;

                    if (!String.IsNullOrEmpty(item.Title))
                    { 
                        title.Value = item.Title; 
                    }

                    row.Cells.Add(source);
                    row.Cells.Add(title);

                    dgvResults.Rows.Add(row);

                    gridRowNbr++;
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            m_cursorOld = this.Cursor;
            this.Cursor = Cursors.WaitCursor;
            search.MergedSearchAsync(tbKeywords.Text, Convert.ToDouble(tbLatitude.Text), Convert.ToDouble(tbLongitude.Text));
        }

        private void dgvResults_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                object o = dgvResults.Rows[e.RowIndex].Cells[0].Tag;
                if (o != null)
                {
                    Type objectType = o.GetType();

                    switch (objectType.Name)
                    {

                        case "RssItem":
                            RssItem item = (RssItem)o;
                            m_cursorOld = this.Cursor; 
                            this.Cursor = Cursors.WaitCursor;
                            webSelectedResult.Navigate(item.Link);

                            break;
                        default:
                            break;
                    }
                }

            }
        }

        private void webSelectedResult_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            this.Cursor = m_cursorOld;
        }
    }
}